# -*- coding: utf-8 -*-
# @Time : 2018/12/25 16:19
# @Author : Administrator
# @File : httptestV5.py
# @Project : python_demo
# @title : SSL-HTTPS-接口文档测试
import hashlib

import requests
import time


class Http_Test():
    def custm(self):  # 定制请求头信息？？？
        url = 'https://api.github.com/some/endpoint'
        r = requests.get(url)
        print(r.headers)
        headers = {'Server': 'woniuxy.com'}
        r = requests.get(url=url, headers=headers)
        print(r.headers)

    def ssl(self):
        r=requests.get('https://kyfw.12306.cn',verify=False)
        print(r.text)
        # r = requests.get('http://github.com', allow_redirects=True)
        # status_code=r.status_code
        # print(status_code)
        # history=r.history
        # print(history)
    def httpstest(self):
        url='https://192.168.2.179/agileone/index.php/common/login'
        data='username=admin&password=admin&savelogin=true'
        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        res=requests.post(url=url,data=data,headers=headers,verify=False)
        print(res.text)

    def redirect(self):
        r = requests.head('http://github.com', allow_redirects=False)
        #默认重定向，301-200；自定义禁止重定向，只会在第一个状态
        print(r.status_code)
        # url=r.url
        # print(url)
        history=r.history
        print(history)
    def test001(self):#发布会ID参数合法
        r=requests.get(url="http://127.0.0.1:8000/api/get_event_list/",params={"eid":"2"})
        print(r.text)
        print(r.json())
        actual=r.json()
        assert actual["status"]==200
        assert actual['message']=="success"
        assert actual["data"]=={'eid': 2, 'name': '红米手机发布会', 'limit': 3000, 'status': True, 'address': '北京', 'start_time': '2016-11-21T15:31:17'}

    def test002(self):  # 发布会ID参数合法
        r = requests.get(url="http://127.0.0.1:8000/api/get_event_list/", params={"eid": ""})
        print(r.text)
        print(r.json())
        actual = r.json()
        assert actual["status"] == 10021
        assert actual['message'] == "parameter error"
    def test003(self):  # 发布会ID参数合法
        r = requests.get(url="http://127.0.0.1:8000/api/get_event_list/", params={"eid": "999"})
        print(r.text)
        print(r.json())
        actual = r.json()
        assert actual["status"] == 10022
        assert actual['message'] == "query result is empty"
    def test004(self):
        r=requests.post(url="http://192.168.2.88:8088/agileone/index.php/common/login",
                        data={"username":"admin","password":"admin","savelogin":"true"})
        print(r.text)
        assert r.text=="successful"
    def test005(self):
        r=requests.post(url="http://192.168.2.88:8088/agileone/index.php/common/login",
                        data={"username":"","password":"admin","savelogin":"true"})
        print(r.text)
        assert r.text=="user_invalid"
    def test006(self):
        auth_user=("admin","admin")
        r=requests.post(url="http://192.168.2.88:8088/agileone/index.php/common/login",
                        data={"username":"admin","password":"","savelogin":"true"},auth=auth_user)
        print(r.text)
        assert r.text=="password_invalid"



    def test_hash(self):
        str_md5="admin"
        str_encode=str_md5.encode()#MD5加密前需转码
        md5=hashlib.md5()#实例化一个MD5的对象
        md5.update(str_encode)#MD5加密
        result=md5.hexdigest()#十六进制输出加密后后的结果
        print(result)
        print(str_md5)

    def test_hash_01(self):
        # Md5(time + api_key),其中api_key = '&Guest-Bugmaster'
        key=time.time()
        print(int(key))
        key1=str(int(key))+'&Guest-Bugmaster'
        md5=hashlib.md5()
        md5.update(key1.encode())
        res=md5.hexdigest()
        print(res)
        data={'eid':'100','name':'xiaomi','limit':'100','address':'chengdu','start_time':'2016-12-31 12:00:00','time':"1500001761",'sign':res}
        resu=requests.post(url="http://127.0.0.1:8000/api/sec_add_event/",
                      data=data)
        print(resu.text)





if __name__ == '__main__':
    Http_Test().test_hash_01()